package edu.uhcl.team_drone.drone;

import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.utils.IntIntMap;

public class InputComponent extends InputAdapter {

    // this is all for handling keypresses
    private final IntIntMap keys = new IntIntMap();
    private int LEFT = Input.Keys.A;
    private int RIGHT = Input.Keys.D;
    private int FORWARD = Input.Keys.W;
    private int BACKWARD = Input.Keys.S;
    private int UP = Input.Keys.UP;
    private int DOWN = Input.Keys.DOWN;
    private int ROTATELEFT = Input.Keys.LEFT;
    private int ROTATERIGHT = Input.Keys.RIGHT;

    private Drone owner;
    private MovementComponent movementCmp;

    public InputComponent(Drone ownerIn, MovementComponent movementIn) {
        this.owner = ownerIn;
        this.movementCmp = movementIn;
    }

    public void update(float deltaTime) {
        // up/down
        if (keys.containsKey(UP)) {
            movementCmp.move(DroneCommands.COMMAND.UP);
        } else if (keys.containsKey(DOWN)) {
            movementCmp.move(DroneCommands.COMMAND.DOWN);
        }
        // left/right
        if (keys.containsKey(LEFT)) {
            movementCmp.move(DroneCommands.COMMAND.LEFT);
        } else if (keys.containsKey(RIGHT)) {
            movementCmp.move(DroneCommands.COMMAND.RIGHT);
        }
        // forward/backward
        if (keys.containsKey(FORWARD)) {
            movementCmp.move(DroneCommands.COMMAND.FORWARD);
        } else if (keys.containsKey(BACKWARD)) {
            movementCmp.move(DroneCommands.COMMAND.BACKWARD);
        }
        // rotations
        if (keys.containsKey(ROTATELEFT)) {
            movementCmp.move(DroneCommands.COMMAND.ROTATE_LEFT);
        } else if (keys.containsKey(ROTATERIGHT)) {
            movementCmp.move(DroneCommands.COMMAND.ROTATE_RIGHT);
        }

    }

    @Override
    public boolean keyDown(int keycode) {
        keys.put(keycode, keycode);
        return true;
    }

    @Override
    public boolean keyUp(int keycode) {
        keys.remove(keycode, 0);
        return true;
    }

}
